package com.bdqn.controller;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONObject;
import com.bdqn.mapper.AppInfoMapper;
import com.bdqn.pojo.AppInfo;
import com.bdqn.pojo.Cha;
import com.bdqn.service.AppInfoService;
import com.bdqn.util.LayData;
import org.apache.ibatis.annotations.Lang;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Date;

@Controller
@RequestMapping("/dev")
public class DevController extends BaseController{
@Resource
    private AppInfoService appInfoService;

    @RequestMapping(value="/tolist",method = RequestMethod.GET)
    public String tolist(){
        return "jsp/dev/list";
    }

    @RequestMapping(value="/toaddus",method = RequestMethod.GET)
    public String toadduser(){
        return "jsp/dev/add";
    }

    @RequestMapping(value = "/upload")
    @ResponseBody
    public String addphone(@RequestParam("file") MultipartFile file) throws IOException {
        String fileName =new Date().getTime()+file.getOriginalFilename().toString();
        FileUtil.writeBytes(file.getBytes(),"C:/Users/student/IdeaProjects/App/src/main/webapp/static/images/"+fileName);
        return JSONObject.toJSONString(LayData.data_upload(1L,fileName));
    }


    @RequestMapping(value = "/upload1")
    @ResponseBody
    public String addphone1(@RequestParam("file") MultipartFile file) throws IOException {
        String fileName =new Date().getTime()+file.getOriginalFilename().toString();
        FileUtil.writeBytes(file.getBytes(),"C:/Users/student/IdeaProjects/App/src/main/webapp/static/wen/"+fileName);
        return JSONObject.toJSONString(LayData.data_upload(1L,fileName));
    }

    @RequestMapping(value = "/toupdate/{oo}")
/*@PathVariable("id")*/
    public String touupdate(@PathVariable("oo") Long oo, Model model) throws IOException {
        AppInfo ap=appInfoService.getOne(oo);
        model.addAttribute("xin",ap);
        return "jsp/dev/update";
    }




    @RequestMapping(value="/download",method=RequestMethod.GET)
    public void download(@RequestParam(value="filename")String filename,
                         HttpServletRequest request,
                         HttpServletResponse response) throws IOException {
        //模拟文件，myfile.txt为需要下载的文件
        String path = request.getSession().getServletContext().getRealPath("static\\wen")+"\\"+filename;
        //获取输入流
        InputStream bis = new BufferedInputStream(new FileInputStream(new File(path)));
        //转码，免得文件名中文乱码
        filename = URLEncoder.encode(filename,"UTF-8");
        //设置文件下载头
        response.addHeader("Content-Disposition", "attachment;filename=" + filename);
        //1.设置文件ContentType类型，这样设置，会自动判断下载文件类型
        response.setContentType("multipart/form-data");
        BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
        int len = 0;
        while((len = bis.read()) != -1){
            out.write(len);
            out.flush();
        }
        out.close();
    }



    @RequestMapping(value="/list",method = RequestMethod.GET)
    @ResponseBody
    public String getUserList(@RequestParam("page") Long from, @RequestParam("limit") Long limit, @RequestParam(value="name",required=false) String name, @RequestParam(value="zhuang",required=false) Long zhuang
    , @RequestParam(value="ping",required=false) Long ping, @RequestParam(value="one",required=false) Long one, @RequestParam(value="two",required=false) Long two, @RequestParam(value="three",required=false) Long three){

        LayData data = appInfoService.getAll(name,zhuang,ping,one,two,three,from,limit);
        return JSONObject.toJSONString(data);
    }
}

